package com.sakura.taos.tool.manipulator.controllers;


import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.HikariDataSourceCreator;
import com.baomidou.dynamic.datasource.ds.GroupDataSource;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.sakura.taos.tool.manipulator.model.*;
import com.sakura.taos.tool.manipulator.model.layui.LayReturn;
import com.sakura.taos.tool.manipulator.model.layui.LayReturnToData;
import com.sakura.taos.tool.manipulator.service.TaosBaseService;
import com.sakura.taos.tool.manipulator.service.TaosSysService;
import com.sakura.taos.tool.manipulator.tool.Cache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.sakura.taos.tool.manipulator.conf.ConstantConf.DATA_BASE_GROUP_MAIN_NAME;
import static com.sakura.taos.tool.manipulator.conf.ConstantConf.DATA_BASE_GROUP_SEPARATOR;
import static com.sakura.taos.tool.manipulator.model.MyEnum.KeyWord.existence;

/**
 * taos 库 操作
 *
 * @Author: 贺坤
 * @Date: 2021/1/28 14:25
 */
@RestController
@RequestMapping("/depot")
@Slf4j
public class TaosDepotController {

    @Autowired
    private TaosBaseService taosBaseService;
    @Resource
    private DynamicRoutingDataSource dataSource;
    @Autowired
    private HikariDataSourceCreator hikariDataSourceCreator;
    @Autowired
    private Cache cache;

    /**
     * 删除库
     *
     * @param dbName
     * @return
     */
    @DeleteMapping
    public LayReturn removeByDbName(String name, String dbName) {
        Integer integer = taosBaseService.removeByDbName(name);
        if (integer == 0) {
            dataSource.removeDataSource(dbName);
        }
        ;
        return integer == 0 ? new LayReturn(0, "成功库删除") : new LayReturn(500, "删除失败" + name + "库失败");
    }

    @PostMapping("{groupName}")
    public LayReturn save(DataBase dataBase, @PathVariable String groupName) {
        // 判断name 是否是关键字
        if (existence(dataBase.getName())) {
            return new LayReturn(500, "添加库失败,name(" + dataBase.getName() + ") 为taos关键字");
        }
        Integer integer = taosBaseService.saveDataBase(dataBase);
        if (false && integer == 0) {
            //  根据组名 添加到动态数据源
            SysDbLink sysDbLink = cache.getMap().get(groupName);
            sysDbLink.setDataBaseName(dataBase.getName());
            MyDataSource dto = sysDbLink.getDataSource();
            DataSourceProperty dataSourceProperty = new DataSourceProperty();
            BeanUtils.copyProperties(dto, dataSourceProperty);
            DataSource dataSource = hikariDataSourceCreator.createDataSource(dataSourceProperty);
            this.dataSource.addDataSource(dto.getPollName(), dataSource);
        }
        log.info("添加库成功");
        return integer == 0 ? new LayReturn(0, "添加成功") : new LayReturn(500, "添加库失败");
    }
}
